<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\plat\controller;

use think\db;

/**
 * 用户控制器
 */
class TenUser extends PlatBase
{

    /**
     * 菜单列表
     */
    public function show()
    {
        return $this->fetch('show');
    }

    /**
     * 选择查看
     */
    public function lookup()
    {
        if (IS_AJAX) {
            $where = $this->logicTenUser->getWhere($this->param);
            $list = $this->logicTenUser->getTenUserList($where, 'b.*');
            return $list;
        }
        //input ids name
        if (!empty($this->param['input_ids'])) {
            $this->assign('input_ids', $this->param['input_ids']);
        } else {
            $this->assign('input_ids', 'input_ids');
        }
        //input texts name
        if (!empty($this->param['input_text'])) {
            $this->assign('input_text', $this->param['input_text']);
        } else {
            $this->assign('input_text', 'input_text');
        }
        //回示区域
        if (!empty($this->param['lookupgroup'])) {
            $this->assign('lookupgroup', $this->param['lookupgroup']);
        } else {
            $this->assign('lookupgroup', 'lookupgroup');
        }
        //input ids name
        if (!empty($this->param['achieve'])) {
            $this->assign('achieve', $this->param['achieve']);
            return $this->fetch('lookup_achieve');
        }
        //选择方式
        if (!empty($this->param['pagetype'])) {
            switch ($this->param['pagetype']) {
                case 'one';
                    return $this->fetch('lookup_one');
            }
        }
        return $this->fetch('lookup');
    }

    //数据浏览
    public function show_json()
    {
        $where = $this->logicTenUser->getWhere($this->param);
        $list = $this->logicTenUser->getTenUserList($where);
        return $list;
    }

    /**
     * 菜单添加
     */
    public function add()
    {
        IS_POST && $this->jump($this->logicTenUser->tenUserAdd($this->param));
        return $this->fetch('add');
    }

    /**
     * 系统用户编辑
     */
    public function edit()
    {

        IS_POST && $this->jump($this->logicTenUser->tenUserEdit($this->param));

        $info = $this->logicTenUser->getTenUserInfo(['b.id' => $this->param['id']]);

        $this->assign('info', $info);

        $this->common_data();

        return $this->fetch('edit');
    }

    /**
     * 系统用户编辑-》个人信息
     */
    public function editInfo()
    {

        IS_POST && $this->jump($this->logicTenUser->tenUserEdit($this->param));

        $info = $this->logicTenUser->getTenUserInfo(['b.id' => $this->param['id']]);

        $this->assign('info', $info);

        return $this->fetch('edit_info');
    }

    /**
     * 系统用户编辑->密码
     */
    public function editPwd()
    {
        IS_POST && $this->jump($this->logicTenUser->tenUserEditPwd($this->param));

        $info = $this->logicTenUser->getTenUserInfo(['b.id' => $this->param['id']]);

        $this->assign('info', $info);

        return $this->fetch('edit_pwd');
    }

    /**
     * 系统用户编辑->重置密码
     */
    public function resetPwd()
    {
        IS_POST && $this->jump($this->logicTenUser->tenUserResetPwd($this->param));
        $where['id'] = ['in', $this->param['id']];
        $username = $this->logicTenUser->getTenUserColumn($where, 'realname');
        $this->assign('username', arr2str($username));
        $this->assign('param', $this->param);
        return $this->fetch('reset_pwd');
    }

    /**
     * 删除
     */
    public function del()
    {
        $this->jump($this->logicTenUser->tenUserDel($this->param));
    }

    /**
     * 会员授权=>权限角色
     */
    public function userRole()
    {

        IS_POST && $this->jump($this->logicTenUser->setTenUserRole($this->param));

        // 所有角色
        $role_list = $this->logicTenRole->getTenRoleList($where = [], true, 'sort asc', false);

        // 当前帐号信息
        $info = $this->logicTenUser->getTenUserInfo(['a.id' => $this->param['id']]);
        $userRoleIds = array_extract($info['role_list'], 'id');//获取当前帐号

        // 选择权限组
        $list = $this->logicTenRole->selectRoleAuthList($role_list, $userRoleIds);
        $this->assign('list', $list);

        $where['id'] = ['in', $this->param['id']];
        $username = $this->logicTenUser->getTenUserColumn($where, 'realname');
        $this->assign('username', arr2str($username));
        $this->assign('param', $this->param);
        return $this->fetch('user_role');
    }

    /**
     * 系统用户->设置职位
     */
    public function userPosition()
    {
        IS_POST && $this->jump($this->logicTenUser->setTenUserPosition($this->param));

        $where['id'] = ['in', $this->param['id']];
        $usernameArr = $this->logicTenUser->getTenUserColumn($where, 'realname');

        $info = $this->logicTenUser->getTenUserInfo(['a.id' => $this->param['id']]);
        $position_list = array_extract($info['position_list'], 'id');

        // 获取部门树结构数据
        $positionTreeList = $this->logicTenPosition->getTenPositionListTree([], '*', 'sort asc', false);
        $positionTreeCheckboxView = $this->logicTenPosition->tenPositionTreeCheckboxView($positionTreeList, 'children', $position_list);
        $this->assign('positionTreeCheckboxView', $positionTreeCheckboxView);

        $this->assign('param', $this->param);
        $this->assign('username', arr2str($usernameArr));

        return $this->fetch('user_position');
    }

    /**
     * 系统用户->设置部门
     */
    public function userDept()
    {
        IS_POST && $this->jump($this->logicTenUser->setTenUserDept($this->param));

        $where['id'] = ['in', $this->param['id']];
        $usernameArr = $this->logicTenUser->getTenUserColumn($where, 'realname');

        $info = $this->logicTenUser->getTenUserInfo(['a.id' => $this->param['id']]);
        $dept_list = array_extract($info['dept_list'], 'id');

        // 获取部门树结构数据
        $deptTreeList = $this->logicTenDept->getTenDeptListTree([], '*', 'sort asc', false);
        $deptTreeCheckboxView = $this->logicTenDept->tenDeptTreeCheckboxView($deptTreeList, 'children', $dept_list);
        $this->assign('deptTreeCheckboxView', $deptTreeCheckboxView);

        $this->assign('param', $this->param);
        $this->assign('username', arr2str($usernameArr));

        return $this->fetch('user_dept');
    }

    /**
     * 会员栏目授权
     */
    public function userMenu()
    {

        IS_POST && $this->jump($this->logicTenUser->setTenUserMenus($this->param));

        //重新得到授权菜单
        $authMenuList = $this->logicTenRoleMenu->getRoleMenuList(TEN_USER_ID,TEN_TENANT_ID);

        // 获取未被过滤的菜单树
        $menu_tree = $this->logicPlatBase->getFilterMenuTree($authMenuList);

        $info = $this->logicTenUser->getTenUserInfo(['b.id' => $this->param['id']]);
        $menuIds=array_extract($info['menu_list'],'ten_menu_id');

        // 菜单转换为多选视图，支持无限级
        $menu_view = $this->logicTenMenu->menuTreeCheckboxView($menu_tree,'child',$menuIds);

        $this->assign('menu_checkbox_view', $menu_view);
        $this->assign('param', $this->param);

        return $this->fetch('user_menu');
    }


    /**
     * 公共数据
     * Author: lingqifei created by at 2020/6/16 0016
     */
    public function common_data()
    {
//        //获取菜单Select结构数据
//        $dept_select = $this->logicSysDept->getSysDeptTreeSelect();
//        $this->assign('dept_select', $dept_select);
//
//        //获取菜单Select结构数据
//        $position_listt = $this->logicSysPosition->getSysPositionTreeSelect();
//        $this->assign('sys_position_list', $position_listt);

    }

    public function get_user_info()
    {
        $userinfo=$this->logicTenUser->getTenUserSubIds(1,'self');
        d($userinfo);
    }
}
